﻿using ProductTool.Entity;
using SmartSql;
using SmartSql.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace ProductTool.Dao
{
    class BrandDao
    {
        ISmartSqlMapper sqlMapper = null;

        public BrandDao()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
        }
        public List<BrandEntity> QueryAllBrand()
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<BrandEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryJDBrand",
                Request = new
                {

                }
            });
            return result.ToList();
        }
        public void CreateBrand(List<BrandEntity> list)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateBrand",
                Request = new
                {
                    brandList= list
                }
            });           
        }
        public CategoryEntity CheckCategoryExists(int category_id)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.QuerySingle<CategoryEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "CheckCategoryExists",
                Request = new
                {
                    category_id
                }
            });
            return result;
        }
        public List<JDCategory> QueryCategory()
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<JDCategory>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryJDCategory",
                Request = new
                {

                }
            });
            return result.ToList();
        }
      
        public void CreateCategory(List<CategoryEntity> list)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateCategory",
                Request = new
                {
                    cateList = list
                }
            });
        }

        public List<BrandEntity> QueryBrand()
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<BrandEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryBrand",
                Request = new
                {

                }
            });
            return result.ToList();
        }

    }
}
